package com.gl.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.gl.entity.EduSubject;
import com.gl.entity.subject.SubjectAdd;
import com.gl.entity.subject.SubjectOne;
import com.gl.entity.subject.SubjectTwo;
import com.gl.mapper.EduSubjectMapper;
import com.gl.service.EduSubjectService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import java.util.ArrayList;
import java.util.List;

/**
 * <p>
 * 课程科目 服务实现类
 * </p>
 *
 * @author testjava
 * @since 2020-12-14
 */
@Service
public class EduSubjectServiceImpl extends ServiceImpl<EduSubjectMapper, EduSubject> implements EduSubjectService {

    //获取所有分类数据，返回一级分类集合，一级分类里面有各自下所有二级分类集合
    @Override
    public List<SubjectOne> getList() {
        //查询属于总分类所有一级分类，parent_id=0
        QueryWrapper<EduSubject> oneWrapper = new QueryWrapper<>();
        oneWrapper.eq("parent_id","0");
        List<EduSubject> oneSubjectList = baseMapper.selectList(oneWrapper);

        //查询属于总分类所有二级分类
        QueryWrapper<EduSubject> twoWrapper = new QueryWrapper<>();
        twoWrapper.ne("parent_id","0");
        List<EduSubject> twoSubjectList = baseMapper.selectList(twoWrapper);

        //最终返回的一级分类集合
        List<SubjectOne> finalSubjectList = new ArrayList<>();

        //遍历所有一级分类数据，加入到最终集合中
        for (EduSubject eduSubject : oneSubjectList) {
            SubjectOne subjectOne = new SubjectOne();
            //复制总分类属性到一级分类属性中
            BeanUtils.copyProperties(eduSubject,subjectOne);
            //最终的一级分类集合添加一级分类
            finalSubjectList.add(subjectOne);

            //创建二级分类集合，最终加入对应一级分类下
            List<SubjectTwo> subjectTwoList=  new ArrayList<>();
            //遍历属于总分类二级分类
            for (EduSubject subject : twoSubjectList) {
                //判断二级分类的父id是否与一级的id一致，一致则添加到二级分类集合
                if(subject.getParentId().equals(eduSubject.getId())){
                    SubjectTwo subjectTwo = new SubjectTwo();
                    //复制总分类属性到二级分类中
                    BeanUtils.copyProperties(subject,subjectTwo);
                    //二级分类集合添加
                    subjectTwoList.add(subjectTwo);
                }
            }
            //把二级分类集合添加到一级分类中，实现联动
            subjectOne.setChildren(subjectTwoList);
        }

        return finalSubjectList;
    }
    //添加课程分类
    @Override
    public void addSubject(SubjectAdd subjectAdd) {

        //判断条件
        String onlyOneSubject = subjectAdd.getOnlyOneSubject();
        String oneSubjectId = subjectAdd.getOneSubjectId();
        String oneSubjectAdd = subjectAdd.getOneSubjectAdd();

        if(!StringUtils.isEmpty(onlyOneSubject)){//仅添加一级分类
            EduSubject subject = new EduSubject();
            //设置父id为0代表一级分类
            subject.setParentId("0");
            subject.setTitle(onlyOneSubject);
            baseMapper.insert(subject);

        }else if(!StringUtils.isEmpty(oneSubjectId)){//仅添加二级分类
            EduSubject subject = new EduSubject();
            //设置二级分类的一级分类id
            subject.setParentId(oneSubjectId);
            subject.setTitle(subjectAdd.getTwoSubject());
            baseMapper.insert(subject);

        }else if(!StringUtils.isEmpty(oneSubjectAdd)){//添加一级二级分类
            //添加一级分类
            EduSubject oneSubject = new EduSubject();
            oneSubject.setParentId("0");
            oneSubject.setTitle(oneSubjectAdd);
            baseMapper.insert(oneSubject);

            String oneId = oneSubject.getId();//获取添加后的一级分类id
            //添加二级分类
            EduSubject twoSubject = new EduSubject();
            twoSubject.setParentId(oneId);
            twoSubject.setTitle(subjectAdd.getTwoSubjectAdd());
            baseMapper.insert(twoSubject);

        }



    }
}
